Package it.czerwinski.kotlin.util

Contains utility types based on Scala.

Types

Either
Link copied to clipboard
common
sealed class Either<out L, out R>
Disjoint union.
Failure
Link copied to clipboard
common
data class Failure(exception: Throwable) : Try<Nothing>
Left
Link copied to clipboard
common
data class Left<out L>(value: L) : Either<L, Nothing>
LeftProjection
Link copied to clipboard
common
data class LeftProjection<out L, out R>(either: Either<L, R>)
None
Link copied to clipboard
common
object None : Option<Nothing>
Representation of a non-existent value.
Option
Link copied to clipboard
common
sealed class Option<out T>
Representation of an optional value.
Right
Link copied to clipboard
common
data class Right<out R>(value: R) : Either<Nothing, R>
RightProjection
Link copied to clipboard
common
data class RightProjection<out L, out R>(either: Either<L, R>)
Some
Link copied to clipboard
common
data class Some<T>(value: T) : Option<T>
Representation of a value of type T.
Success
Link copied to clipboard
common
data class Success<out T>(value: T) : Try<T>
Try
Link copied to clipboard
common
sealed class Try<out T>
Representation of an operation that might successfully return a value or throw an exception.

Functions

asOption
Link copied to clipboard
common
fun <T> T?.asOption(): Option<T>
Returns Some if this is not null or None if this is null.
contains
Link copied to clipboard
common
operator fun <T> Option<T>.contains(element: T): Boolean
Tests whether the Option contains the given element.
evert
Link copied to clipboard
common
fun <T> Option<Try<T>>.evert(): Try<Option<T>>
Moves inner Try outside of the outer Option.
fun <T> Try<Option<T>>.evert(): Option<Try<T>>
Moves inner Option outside of the outer Try.
filterNotNull
Link copied to clipboard
common
fun <L, R> Either<L, R?>.filterNotNull(): Either<L, R>?
Returns the same Right if its value is not null.
fun <L, R> LeftProjection<L?, R>.filterNotNull(): Either<L, R>?
Returns the same Left if its value is not null.
fun <L, R> RightProjection<L, R?>.filterNotNull(): Either<L, R>?
Returns the same Right if its value is not null.
fun <T> Try<T?>.filterNotNull(): Try<T>
Returns the same Success if its value is not null.
filterNotNullToOption
Link copied to clipboard
common
fun <L, R> Either<L, R?>.filterNotNullToOption(): Option<Either<L, R>>
Returns Some containing the same Right if its value is not null.
fun <L, R> LeftProjection<L?, R>.filterNotNullToOption(): Option<Either<L, R>>
Returns Some containing the same Left if its value is not null.
fun <L, R> RightProjection<L, R?>.filterNotNullToOption(): Option<Either<L, R>>
Returns Some containing the same Right if its value is not null.
filterOrElse
Link copied to clipboard
common
inline fun <L, R> Either<L, R>.filterOrElse(predicate: (R) -> Boolean, zero: () -> L): Either<L, R>?
Returns the same Right if the predicate is satisfied for the value, Left(zero) if the predicate is not satisfied for the value, or the same Left if this is Left.
inline fun <L, R> LeftProjection<L, R>.filterOrElse(predicate: (L) -> Boolean, zero: () -> R): Either<L, R>?
Returns the same Left if the predicate is satisfied for the value, Right(zero) if the predicate is not satisfied for the value, or the same Right if this is Right.
inline fun <L, R> RightProjection<L, R>.filterOrElse(predicate: (R) -> Boolean, zero: () -> L): Either<L, R>?
Returns the same Right if the predicate is satisfied for the value, Left(zero) if the predicate is not satisfied for the value, or the same Left if this is Left.
flatMap
Link copied to clipboard
common
inline fun <L, R, T> Either<L, R>.flatMap(transform: (R) -> Either<L, T>): Either<L, T>
Maps value of this Right to a new Either using transform.
inline fun <L, R, T> LeftProjection<L, R>.flatMap(transform: (L) -> Either<T, R>): Either<T, R>
Maps value of this Left to a new Either using transform.
inline fun <L, R, T> RightProjection<L, R>.flatMap(transform: (R) -> Either<L, T>): Either<L, T>
Maps value of this Right to a new Either using transform.
flatten
Link copied to clipboard
common
fun <T> Option<Option<T>>.flatten(): Option<T>
Transforms a nested Option to a not nested Option.
fun <T> Option<Try<T>>.flatten(): Option<T>
Returns Some if this Some contains a Success.
fun <T> Option<Iterable<T>>.flatten(): List<T>
Returns nested List if this is Some.
fun <T> Try<Option<T>>.flatten(): Option<T>
Extracts an Option nested in the Try to a not nested Option.
fun <T> Try<Try<T>>.flatten(): Try<T>
Transforms a nested Try to a not nested Try.
fun <T> Iterable<Option<T>>.flatten(): List<T>
Returns List of values of each Some in this Iterable.
getOrElse
Link copied to clipboard
common
inline fun <L, R> Either<L, R>.getOrElse(default: () -> R): R
Gets value of this Right or default value if this is Left.
inline fun <L, R> LeftProjection<L, R>.getOrElse(default: () -> L): L
Gets value of this Left or default value if this is Right.
inline fun <T> Option<T>.getOrElse(default: () -> T): T
Gets the value of a Some or default value if this is None.
inline fun <L, R> RightProjection<L, R>.getOrElse(default: () -> R): R
Gets value of this Right or default value if this is Left.
inline fun <T> Try<T>.getOrElse(default: () -> T): T
Gets the value of a Success or default value if this is a Failure.
joinLeft
Link copied to clipboard
common
fun <L, R> Either<Either<L, R>, R>.joinLeft(): Either<L, R>
Returns this if this is Right.
joinRight
Link copied to clipboard
common
fun <L, R> Either<L, Either<L, R>>.joinRight(): Either<L, R>
Returns this if this is Left.
merge
Link copied to clipboard
common
fun <T> Either<T, T>.merge(): T
Merges Left and Right of the same type to a single value.
orElse
Link copied to clipboard
common
inline fun <T> Option<T>.orElse(default: () -> Option<T>): Option<T>
Returns this Option if this is a Some or default if this is None.
inline fun <T> Try<T>.orElse(default: () -> Try<T>): Try<T>
Returns this Try if this is a Success or default if this is a Failure.
recover
Link copied to clipboard
common
inline fun <T> Try<T>.recover(rescue: (Throwable) -> T): Try<T>
Returns this Try if this is a Success or a Try created for the rescue operation if this is a Failure.
recoverWith
Link copied to clipboard
common
inline fun <T> Try<T>.recoverWith(rescue: (Throwable) -> Try<T>): Try<T>
Returns this Try if this is a Success or a Try created by the rescue function if this is a Failure.
unzip
Link copied to clipboard
common
fun <A, B> Option<Pair<A, B>>.unzip(): Pair<Option<A>, Option<B>>
Transforms an Option of a Pair into a Pair of an Option of the first value and an Option of the second value.
fun <A, B, C> Option<Triple<A, B, C>>.unzip(): Triple<Option<A>, Option<B>, Option<C>>
Transforms an Option of a Triple into a Triple of an Option of the first value, an Option of the second value, and an Option of the third value.